/**
 * @file IIC.c
 * @author your name (you@domain.com)
 * @brief IIC通讯源文件
 * @version 0.1
 * @date 2024-03-31
 * 
 * @copyright Copyright (c) 2024
 * 
 */
#include "IIC.h"

/**
 * @brief IIC1硬件初始化
 * 
 */
void IIC1_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStructure);


    I2C_InitTypeDef I2C_InitStructure;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = 100000;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    I2C_InitStructure.I2C_OwnAddress1 = 0X78;
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_Init(I2C1,&I2C_InitStructure);

    I2C_Cmd(I2C1,ENABLE);
}

/**
 * @brief 等待标志位
 * 
 * @param I2Cx iic硬件外设
 * @param I2C_EVENT 标志位类型
 */
void IIC_Per_WaitEvent(I2C_TypeDef* I2Cx,uint32_t I2C_EVENT)
{
    uint32_t Timeout = 10000;
    while (I2C_CheckEvent(I2Cx,I2C_EVENT) != SUCCESS)
    {
        Timeout--;
        if (Timeout == 0)
        {
            break;
        }
        
    }
    
}


/**
 * @brief IIC发送数据
 * 
 * @param I2Cx iic硬件外设
 * @param PerAddress 通讯硬件地址
 * @param RegAddress 寄存器地址
 * @param Data 数据
 */
void IIC_Per_WriteReg(I2C_TypeDef* I2Cx,uint8_t PerAddress,uint8_t RegAddress,uint8_t Data)
{
    I2C_GenerateSTART(I2Cx,ENABLE);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT);

    I2C_Send7bitAddress(I2Cx,PerAddress,I2C_Direction_Transmitter);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);

    I2C_SendData(I2Cx,RegAddress);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTING);

    I2C_SendData(I2Cx,Data);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED);

    I2C_GenerateSTOP(I2Cx,ENABLE);

}

/**
 * @brief IIC连续发送数据
 * 
 * @param I2Cx iic硬件外设
 * @param PerAddress 通讯硬件地址
 * @param RegAddress 寄存器地址
 * @param ArrData 数据数组
 * @param Length 数组长度
 */
void IIC_Per_WriteReg_Coiled(I2C_TypeDef* I2Cx,uint8_t PerAddress,uint8_t RegAddress,uint8_t* ArrData,uint16_t Length)
{
    uint16_t i = 0;
    I2C_GenerateSTART(I2Cx,ENABLE);

    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT);//EV5
    I2C_Send7bitAddress(I2Cx,PerAddress,I2C_Direction_Transmitter);

    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);//EV6
    I2C_SendData(I2Cx,RegAddress);

    
    for ( i = 0; i < Length; i++)
    {
        IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTING);//EV8
        I2C_SendData(I2Cx,*(ArrData+i));
    }
    
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED);//EV8_2
    I2C_GenerateSTOP(I2Cx,ENABLE);

}

/**
 * @brief IIC读取数据
 * 
 * @param I2Cx iic硬件外设
 * @param PerAddress 通讯硬件地址
 * @param RegAddress 寄存器地址
 * @return uint8_t 读取数据
 */
uint8_t IIC_Per_ReadReg(I2C_TypeDef* I2Cx,uint8_t PerAddress,uint8_t RegAddress)
{
    uint8_t Data;
    I2C_GenerateSTART(I2Cx,ENABLE);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT);

    I2C_Send7bitAddress(I2Cx,PerAddress,I2C_Direction_Transmitter);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);

    I2C_SendData(I2Cx,RegAddress);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_BYTE_TRANSMITTED);

    I2C_GenerateSTART(I2Cx,ENABLE);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_MODE_SELECT);

    I2C_Send7bitAddress(I2Cx,PerAddress,I2C_Direction_Receiver);
    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED);

    I2C_AcknowledgeConfig(I2Cx,DISABLE);
    I2C_GenerateSTOP(I2Cx,ENABLE);

    IIC_Per_WaitEvent(I2Cx,I2C_EVENT_MASTER_BYTE_RECEIVED);
    Data = I2C_ReceiveData(I2Cx);

    I2C_AcknowledgeConfig(I2Cx,ENABLE);
    return Data;
}







